"use client"

// 本地存储管理
export class LocalStorage {
  // 保存用户信息
  static saveUser(user: any) {
    if (typeof window !== "undefined") {
      localStorage.setItem("business_hub_user", JSON.stringify(user))
    }
  }

  // 获取用户信息
  static getUser() {
    if (typeof window !== "undefined") {
      const userStr = localStorage.getItem("business_hub_user")
      return userStr ? JSON.parse(userStr) : null
    }
    return null
  }

  // 清除用户信息
  static clearUser() {
    if (typeof window !== "undefined") {
      localStorage.removeItem("business_hub_user")
    }
  }

  // 保存阅读历史
  static saveReadHistory(newsId: string) {
    if (typeof window !== "undefined") {
      const history = this.getReadHistory()
      const newHistory = [newsId, ...history.filter((id) => id !== newsId)].slice(0, 100)
      localStorage.setItem("business_hub_read_history", JSON.stringify(newHistory))
    }
  }

  // 获取阅读历史
  static getReadHistory(): string[] {
    if (typeof window !== "undefined") {
      const historyStr = localStorage.getItem("business_hub_read_history")
      return historyStr ? JSON.parse(historyStr) : []
    }
    return []
  }

  // 保存收藏
  static saveBookmark(newsId: string) {
    if (typeof window !== "undefined") {
      const bookmarks = this.getBookmarks()
      const newBookmarks = [...bookmarks, newsId]
      localStorage.setItem("business_hub_bookmarks", JSON.stringify(newBookmarks))
    }
  }

  // 移除收藏
  static removeBookmark(newsId: string) {
    if (typeof window !== "undefined") {
      const bookmarks = this.getBookmarks()
      const newBookmarks = bookmarks.filter((id) => id !== newsId)
      localStorage.setItem("business_hub_bookmarks", JSON.stringify(newBookmarks))
    }
  }

  // 获取收藏列表
  static getBookmarks(): string[] {
    if (typeof window !== "undefined") {
      const bookmarksStr = localStorage.getItem("business_hub_bookmarks")
      return bookmarksStr ? JSON.parse(bookmarksStr) : []
    }
    return []
  }

  // 保存用户偏好设置
  static saveSettings(settings: any) {
    if (typeof window !== "undefined") {
      localStorage.setItem("business_hub_settings", JSON.stringify(settings))
    }
  }

  // 获取用户偏好设置
  static getSettings() {
    if (typeof window !== "undefined") {
      const settingsStr = localStorage.getItem("business_hub_settings")
      return settingsStr
        ? JSON.parse(settingsStr)
        : {
            notifications: true,
            autoUpdate: true,
            darkMode: false,
            fontSize: "medium",
            theme: "blue",
          }
    }
    return {}
  }
}
